package com.design_pattern.responsibility;

import java.util.ArrayList;
import java.util.List;

public class FilterChain implements Filter {
    private int index = 0;
    private List<Filter> filterList = new ArrayList<>();


    public List<Filter> getFilterList() {
        return filterList;
    }

    public FilterChain addFilter(Filter filter) {
        this.filterList.add(filter);
        return this;
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {
        if (index == filterChain.filterList.size()) {
            return;
        }
        Filter filter = filterList.get(index);
        index++;
        filter.doFilter(request, response, filterChain);

    }
}
